"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var yfieldsControls_1 = __importDefault(require("../yfieldsControls"));
Component({
    properties: {
        rules: Object,
    },
    data: {
        form: {},
        label: {}
    },
    relations: {
        '../yfieldsControls': {
            type: 'descendant',
            target: yfieldsControls_1.default,
            linked: function (target) {
                this.setData({ form: Object.assign(this.data.form, target.dataset) });
                var label = {};
                var labelName = Object.keys(target.dataset)[0];
                var laeblValue = target.data.label;
                label[labelName] = laeblValue;
                this.setData({ label: Object.assign(this.data.label, label) });
            }
        }
    },
    methods: {
        validate: function () {
            for (var key in this.data.rules) {
                // 判断form表单中是否存在这个数据
                if (!(key in this.data.form)) {
                    return false;
                }
                var formValue = this.data.form[key];
                // 判断这个数据是否是required
                if (this.data.rules[key].require.value) {
                    if (formValue == '') {
                        var errMsg = this.data.rules[key].require.errMsg || this.data.label[key] + "\u4E0D\u80FD\u4E3A\u7A7A";
                        wx.showToast({ title: errMsg, icon: "none" });
                        return false;
                    }
                }
                // 判断这个数据的其他正则
                if (this.data.rules[key].re) {
                    var re = new RegExp(this.data.rules[key].re.value);
                    if (!re.test(formValue)) {
                        var errMsg = this.data.rules[key].re.errMsg || this.data.label[key] + "匹配规则失败,未配置规则错误提示";
                        wx.showToast({ title: errMsg, icon: "none" });
                        return false;
                    }
                }
            }
            return true;
        },
        onSubmit: function () {
            if (!this.validate())
                return;
            this.triggerEvent("onSubmit", this.data.form, {});
        },
    },
});
